<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        #webcam {
            border: 1px solid #666666;
            width: 320px;
            height: 240px;
        }

        #photos {
            border: 1px solid #666666;
            width: 320px;
            height: 240px;
        }

        .btn {
            width: 320px;
            height: auto;
            margin: 5px 0px;
        }

        .btn input[type=button] {
            width: 150px;
            height: 50px;
            line-height: 50px;
            margin: 3px;
        }
    </style>
</head>

<body>
    <div id="webcam"></div>
    <div class="btn">
        <input type="button" value="删除" id="delBtn" onclick="delPhoto();" />
        <input type="button" value="拍照" id="saveBtn" onclick="take_snapshot();" />
    </div>
    <div id="photos">
        <img src="" id="img">
    </div>
    <script type="text/javascript" src="./js/jquery.js"></script>
    <script type="text/javascript" src="./webcamjs-master/webcam.js"></script>
    <script type="text/javascript">
    var saveCB;
        $(function () {
            var w = 320,
                h = 240;
            var pos = 0,
                ctx = null,
                 image = [];

            var canvas = document.createElement("canvas");
            canvas.setAttribute('width', w);
            canvas.setAttribute('height', h);

            console.log(canvas.toDataURL);
            if (canvas.toDataURL) {

                ctx = canvas.getContext("2d");

                image = ctx.getImageData(0, 0, w, h);
                $("#img").attr("src", "${ctx}/" + msg);
                saveCB = function (data) {

                    var col = data.split(";");
                    var img = image;

                    for (var i = 0; i < w; i++) {
                        var tmp = parseInt(col[i]);
                        img.data[pos + 0] = (tmp >> 16) & 0xff;
                        img.data[pos + 1] = (tmp >> 8) & 0xff;
                        img.data[pos + 2] = tmp & 0xff;
                        img.data[pos + 3] = 0xff;
                        pos += 4;
                    }

                    if (pos >= 4 * w * h) {
                        ctx.putImageData(img, 0, 0);                      
                    }
                };

            }

           
        });
        Webcam.set({
			width: 320,
			height: 240,
			image_format: 'jpeg',
            jpeg_quality: 100,
            onSave:saveCB,
            onCapture: function () {
                webcam.save();
            }
		});
		Webcam.attach( '#webcam' );
        //拍照
        function savePhoto() {
            webcam.capture();
        }
        function take_snapshot() {
			// take snapshot and get image data
			Webcam.snap( function(data_uri) {
				// display results in page
				document.getElementById('results').innerHTML = 
					'<h2>Here is your image:</h2>' + 
					'<img src="'+data_uri+'"/>';
			} );
		}
        /*$(function () {
    var image = new Array();
    var w = 320, h = 240;
    var pos = 0;
    $("#webcam").webcam({
        width: w,
        height: h,
        mode: "callback",
        swffile: "${ctxStatic }/jquery-plugin/jscam_canvas_only.swf", // canvas only doesn't implement a jpeg encoder, so the file is much smaller

        onTick: function (remain) {
            if (0 == remain) {
                jQuery("#status").text("Cheese!");
            } else {
                jQuery("#status").text(remain + " seconds remaining...");
            }
        },

        onSave: function (data) {
            // Work with the picture. Picture-data is encoded as an array of arrays... Not really nice, though =/
            image.push(data);
            pos += 4 * w;
            if (pos == 4 * w * h) {
                $.post("${ctx}/common/webcam/uploadPhoto", {w: w, h: h, image: image.join('|')}, function (msg) {
                    pos = 0;
                    image = new Array();
                    $("#img").attr("src", "${ctx}/"+msg);
                });
            }
        },

        onCapture: function () {
            webcam.save();
            // Show a flash for example
        },

        debug: function (type, string) {
            // Write debug information to console.log() or a div, ...
        },

        onLoad: function () {
            // Page load
            var cams = webcam.getCameraList();
            for (var i in cams) {
                jQuery("#cams").append("<li>" + cams[i] + "</li>");
            }
        }
    });
});*/
    </script>
</body>

</html>